package AStar;

import java.util.List;
import java.util.ArrayList;

public class AStarImpl implements Navigation.IAStar {
	
	private List<AStarNode> openList;
	private List<AStarNode> closedList;
	private AStarNode finishNode;
	private AStarNode startNode;
	
	public AStarImpl(AStarNode start, AStarNode finish) {
		openList = new ArrayList<AStarNode>();
		closedList = new ArrayList<AStarNode>();
		openList.add(start);
		this.finishNode = finish;
		this.startNode = start;
	}

	public AStarNode[] run() {
		while(!openList.isEmpty()){
			AStarNode currentNode = getMinNode();
			if(currentNode == finishNode){
				return getShortestPath();
			}
			expandNode(currentNode);
			closedList.add(currentNode);
		}
		return null; // no Path found
	}

	private AStarNode[] getShortestPath(){
		List<AStarNode> shortestPath = new ArrayList<AStarNode>();
		AStarNode currentNode = finishNode;
		while(currentNode != startNode){
			shortestPath.add(currentNode);
			currentNode = currentNode.getPreviousNode();
		}
		return shortestPath.toArray(new AStarNode[]{});
	}
	
	private AStarNode getMinNode(){
		int lowestF = Integer.MAX_VALUE;
		AStarNode minNode = null;
		for(AStarNode node : openList){
			if(node.getF() < lowestF){
				minNode = node;
			}
		}
		openList.remove(minNode);
		return minNode;
	}
	
	private void expandNode(AStarNode node){
		
	}
}